package com.example.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.entity.Temperature;
import com.example.mapper.TemperatureMapper;
import com.example.model.ReturnNo;
import com.example.model.ReturnObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("line/temperature")
public class TemperatureController {
    @Resource
    TemperatureMapper temperatureMapper;

    @GetMapping
    public List<Temperature> getTemperatureDate(){
        return temperatureMapper.selectList(Wrappers.lambdaQuery());
    }

    @PostMapping
    public ReturnObject addTemperatureData(@RequestBody Temperature temperature){
        if(temperature.getHighTemp() < temperature.getLowTemp()){
            return new ReturnObject(ReturnNo.TEMPERATURE_INVALID);
        }
        temperature.setDate(new Date());
        if(temperatureMapper.insert(temperature) == 1){
            return new ReturnObject(ReturnNo.CREATED);
        } else {
            return new ReturnObject(ReturnNo.CREATE_FAILED);
        }
    }
}
